package com.pzj.swtcallcom.ocx.ie;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.OleListener;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Composite;

import com.pzj.swtcallcom.ocx.SuperOcx;

/**
 * IE控制
 * 
 */
public class IEFrame extends SuperOcx {
	public AutomationClientSite automationClientSite;
	/**
	 * session event
	 */
	public static final int OnStatusChanged = 1;//

	/**
	 * Create the composite
	 * 
	 * @param parent
	 * @param style
	 */
	public IEFrame(Composite parent, int style) {
		super(parent, style);
		createContents();
	}

	protected void createContents() {
		setLayout(new FillLayout());
		try {
			OleFrame frame = new OleFrame(this, SWT.NO_TRIM);
			automationClientSite = new AutomationClientSite(frame, SWT.NONE,
					"InternetExplorer.Application");
			player = new OleAutomation(automationClientSite);
			int[] ids = player.getIDsOfNames(new String[] { "visible" });
			player.setProperty(ids[0], new Variant(true));
		} catch (SWTException e) {
			throw new RuntimeException(e.getMessage(), e);
		} catch (SWTError e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	/**
	 * 跳转网址
	 * 
	 * @param rgvarg
	 * @param rgdispidNamedArgs
	 * @return
	 */
	public long Navigate(String rgvarg, int rgdispidNamedArgs) {
		Variant v[] = new Variant[2];
		v[0] = new Variant(rgvarg);
		v[1] = new Variant(rgdispidNamedArgs);
		return execute("Navigate", v).getLong();
	}

	/**
	 * 用来获取dom的元素
	 * 
	 * @param id
	 * @return
	 */
	public OleAutomation getElementById(String id) {
		Variant v[] = new Variant[1];
		v[0] = new Variant(id);
		return execute("getElementById", v).getAutomation();
	}

	@Override
	public void addEventListener(int eventID, OleListener listener) {
		super.addEventListener(eventID, listener);
	}

	@Override
	public void removeEventListener(int eventID, OleListener listener) {
		super.removeEventListener(eventID, listener);
	}
}
